<?php

namespace app\api\controller;

use app\common\library\common;
use think\facade\Db;
use think\Request;
use think\Response;

class Meituan extends Wap
{
    /**
     * 美团兑换
     * @param Request $request
     */


    public function index(Request $request)
    {
        if (request()->isPost()) {

            $user = $this->checkToken();
            $post = $request->param();
            $receipt_code = trim($post['receipt_code']);  //核销码
            // 查询门店
            $room_list = Db::name('customer_meituan_config')->where('store_id', $post['room_id'])->find();
            if (!$room_list) {
                $arr['code'] = 101;
                $arr['message'] = '当前门店未配置美团验券信息,请切换门店后验券';
                Response::create($arr, 'json')->send();
                exit();
            }

            if (isset($room_list['appkey'])) {

                // 查询
                $datac['app_key'] = $room_list['appkey'];
                $datac['timestamp'] = date("Y-m-d H:i:s");
                $datac['session'] = $room_list['session'];
                $datac['format'] = 'json';
                $datac['v'] = '1';
                $datac['open_shop_uuid'] = $room_list['open_shop_uuid'];
                $datac['sign_method'] = 'MD5';
                $datac['qr_code'] = $post['receipt_code'];
                //加密
                $sign = common::getSign($datac);
                $app_secret = $room_list['secret'];
                $sign = $app_secret . $sign . $app_secret;
                $sign = md5($sign);
                $datac['sign'] = $sign;
                $header = array(
                    'Content-Type:' . 'application/x-www-form-urlencoded; charset=UTF-8'
                );
                $url = "https://openapi.dianping.com/router/tuangou/receipt/scanprepare";

                if (!isset($datac['app_key'])) {
                    $arr['code'] = 0;
                    $arr['message'] = '获取配置信息失败';
                    Response::create($arr, 'json')->send();
                    exit();
                }

                $res = common::curl_post($url, $datac, $header);
                $return = json_decode($res, true);

                $card_list = array();
            
                if ($return['code'] == 200) {

                    $dealgroup_id = 0;
                    if (isset($return['data'][0]['dealgroup_id'])) {
                        if (isset($return['data'][0]['dealgroup_id'])) $dealgroup_id = trim($return['data'][0]['dealgroup_id']);   //美团商品id
                        if (isset($return['data'][0]['deal_price'])) $deal_price = $return['data'][0]['deal_price']; //商品售卖价格

                        $card_where['meituan_id'] = $dealgroup_id;
                        $card_where['mark'] = 1;
                        if($post['coupontype']==2){
                            $card_list = Db::name('pricecoupon')->where($card_where)->find();
                        }else {
                            $card_list = Db::name('coupon')->where($card_where)->find();
                            }

                    } else {
                        //拼团
                        if (isset($return['data'][0]['product_item_id'])) $product_item_id = $return['data'][0]['product_item_id'];
                        if (isset($return['data'][0]['deal_price'])) $deal_price = $return['data'][0]['deal_price']; //商品售卖价格
                        //$product_item_id = $return['data'][0]['product_item_id'];
                        $card_where['meituan_id'] = $product_item_id;
                        $card_where['mark'] = 1;
                        if($post['coupontype']==2){
                            $card_list = Db::name('pricecoupon')->where($card_where)->find();
                        }else {
                            $card_list = Db::name('coupon')->where($card_where)->find();
                        }
                    }
                    if (!$card_list) {

                        $arr['code'] = 0;
                        $arr['message'] = '暂无兑换的优惠卡,请稍后再试';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                } else {
                    $arr['code'] = 0;
                    $arr['message'] = $return['msg'];
                    Response::create($arr, 'json')->send();
                    exit();
                }
            }
            if (isset($room_list['appkey'])) {



                //兑换
                $data['app_key'] = $room_list['appkey'];
                $data['timestamp'] = date("Y-m-d H:i:s");
                $data['session'] = $room_list['session'];
                $data['format'] = 'json';
                $data['v'] = '1';
                $data['sign_method'] = 'MD5';
                $data['requestid'] = rand(1000, 9999);
                $data['receipt_code'] = $receipt_code;
                $data['count'] = '1';
                $data['open_shop_uuid'] = $room_list['open_shop_uuid'];
                $data['app_shop_account'] = $room_list['app_shop_account'];
                $data['app_shop_accountname'] = $room_list['app_shop_accountname'];
                //加密
                $sign = common::getSign($data);
                $app_secret = $room_list['secret'];
                $sign = $app_secret . $sign . $app_secret;
                $sign = md5($sign);
                $data['sign'] = $sign;
                $header = array(
                    'Content-Type:' . 'application/x-www-form-urlencoded; charset=UTF-8'
                );
                $url = "https://openapi.dianping.com/router/tuangou/receipt/consume";
                if (!isset($data['app_key'])) {
                    $arr['code'] = 0;
                    $arr['message'] = '当前门店未配置美团验券信息,请切换门店后验券';
                    Response::create($arr, 'json')->send();
                    exit();
                }
                $res = common::curl_post($url, $data, $header);
                $return = json_decode($res, true);


                if ($return['code'] == 200) {
                    $dealgroup_id = 0;

                    //团购
                    if (isset($return['data'][0]['dealgroup_id'])) {
                        if (isset($return['data'][0]['dealgroup_id'])) $dealgroup_id = trim($return['data'][0]['dealgroup_id']);   //美团商品id
                        if (isset($return['data'][0]['deal_price'])) $deal_price = $return['data'][0]['deal_price']; //商品售卖价格
                        $card_where['meituan_id'] = $dealgroup_id;
                        $card_where['mark'] = 1;
                        if($post['coupontype']==2){
                            $card_list = Db::name('pricecoupon')->where($card_where)->find();
                        }else {
                            $card_list = Db::name('coupon')->where($card_where)->find();
                        }
                    } else {
                        //拼团
                        if (isset($return['data'][0]['product_item_id'])) $product_item_id = $return['data'][0]['product_item_id'];
                        if (isset($return['data'][0]['deal_price'])) $deal_price = $return['data'][0]['deal_price']; //商品售卖价格
                        $card_where['meituan_id'] = $dealgroup_id;
                        $card_where['mark'] = 1;
                        if($post['coupontype']==2){
                            $card_list = Db::name('pricecoupon')->where($card_where)->find();
                        }else {
                            $card_list = Db::name('coupon')->where($card_where)->find();
                        }
                    }

                    $dataCoupon = array(
                        'create_time' => time(),
                        'coupon_id' => $card_list['id'],
                        'user_id' => $user['id'],
                        'type' => 2,
                        'end_time' => $card_list['end_time'],
                        'add_time' => $card_list['add_time'],
                        'price' => $card_list['price'],
                        'store_id' => $post['room_id'],
                    );
                    $insert['user_id'] = $user['id'];
                    $insert['receipt_code'] = $receipt_code;
                    $insert['status'] = 0;
                    $insert['content'] = $res;
                    $insert['admin_id'] = $post['room_id'];
                    $insert['create_time'] = time();
                    $insert['crad_id'] = $card_list['id'];
                    DB::name('mini_code_list')->insert($insert);

                    if($post['coupontype']==2){
                        $dataCoupon['hours_add_time']=$card_list['hours_add_time'];
                        $dataCoupon['hours_end_time']=$card_list['hours_end_time'];
                        $dataCoupon['shichang']=$card_list['shichang'];
                        $dataCoupon['course_id']=$card_list['course_id'];
                        $dataCoupon['order_on']=$receipt_code;
                        if (Db::name('mini_user_pricecoupon')->insert($dataCoupon)) {
                            // 更新领取数量
                            $coi = array(
                                'surplus_num' => $card_list['num'] - $card_list['receive_num'] - 1,
                                'receive_num' => $card_list['receive_num'] + 1,
                            );

                            Db::name('pricecoupon')->where('id', $card_list['id'])->update($coi);
                            $arr['code'] = 200;
                            $arr['message'] = '核销成功';
                            Response::create($arr, 'json')->send();
                            exit();
                        }

                    }else{
                        if (Db::name('mini_user_coupon')->insert($dataCoupon)) {
                            // 更新领取数量
                            $coi = array(
                                'surplus_num' => $card_list['num'] - $card_list['receive_num'] - 1,
                                'receive_num' => $card_list['receive_num'] + 1,
                            );

                            Db::name('coupon')->where('id', $card_list['id'])->update($coi);
                            $arr['code'] = 200;
                            $arr['message'] = '核销成功';
                            Response::create($arr, 'json')->send();
                            exit();
                        }

                    }

                } else {
                    $arr['code'] = 0;
                    $arr['message'] = '核销失败';
                    Response::create($arr, 'json')->send();
                    exit();
                }


            } else {
                $arr['code'] = 0;
                $arr['message'] = '获取配置信息失败';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }


    /**
     * 获取美团配置
     * @param Request $request
     */
    public function detail(Request $request)
    {
        if (request()->isPost()) {
            $admin_id = $this->checkAdmin();
            $post = $request->param();
            $info = Db::name('mini_meituan')->where('admin_id', $admin_id)->find();
            $data['content'] = $info['content'];
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


}